package com.lib.common.view;

import android.content.Context;
import android.util.AttributeSet;
import android.widget.ScrollView;

import com.lib.utils.ConvertUtils;
import com.lib.utils.XLog;

/**
 * @author 一叶丶枫凌
 * Created on 2017-06-26 16:36
 */
public class FlexScrollView extends ScrollView {
    private OnScrollChangedListener mOnScrollChangedListener;

    public interface OnScrollChangedListener {

        void onScrollChanged(int scrollY);
    }

    public void setOnScrollChangedListener(OnScrollChangedListener l) {
        mOnScrollChangedListener = l;
    }

    public FlexScrollView(Context context) {
        this(context, null);
    }

    public FlexScrollView(Context context, AttributeSet attrs) {
        this(context, attrs, 0);
    }

    public FlexScrollView(Context context, AttributeSet attrs, int defStyle) {
        super(context, attrs, defStyle);
    }

    @Override
    protected void onScrollChanged(int l, int t, int oldl, int oldt) {
        super.onScrollChanged(l, t, oldl, oldt);
        if (mOnScrollChangedListener != null) {
            mOnScrollChangedListener.onScrollChanged(t);
        }
    }

    @Override
    protected boolean overScrollBy(int deltaX, int deltaY, int scrollX, int scrollY, int scrollRangeX, int scrollRangeY,
                                   int maxOverScrollX, int maxOverScrollY, boolean isTouchEvent) {
        // 实现的本质就是在这里动态改变了maxOverScrollY的值
        maxOverScrollY = ConvertUtils.dp2px(XLog.sContext, 50);
        return super.overScrollBy(deltaX, deltaY, scrollX, scrollY, scrollRangeX, scrollRangeY,
                maxOverScrollX, maxOverScrollY, isTouchEvent);
    }
}